package com.naiterui.ehp.bs.cms.modules.wms.controller;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.wms.feign.IB2cPlatformFeignClient;
import com.naiterui.ehp.bs.cms.modules.wms.service.IPlatformService;
import com.naiterui.ehp.bs.cms.modules.wms.vo.PlatformSkuVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.PrescriptionPlatformProductVO;
import com.naiterui.ehp.bs.cms.modules.wms.vo.ThirdPartyProductVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * <pre>
 * 处方流转相关业务接口
 * </pre>
 *
 * @author fangguanhong
 * @date 2021/4/8 3:41 PM
 * @since 1.0.0
 */
@Slf4j
@Validated
@RestController
@Api(tags = {SwaggerTag.CMS + SwaggerTag.MEDICINE_DRUG})
@RequestMapping("wms/platform/sku")
public class PlatformController {

    @Autowired
    private IB2cPlatformFeignClient b2cPlatformFeignClient;
    @Autowired
    private IPlatformService platformService;

    @SysLog("处方流转-商品列表")
    @ApiOperation(value = "处方流转-商品列表")
    @GetMapping("list")
    public PageVO<PrescriptionPlatformProductVO> platformProductList(PageParamsVO pageParamsVO, PrescriptionPlatformProductVO platformProductVO)
        throws BusinessException {
        return this.platformService.getPlatformProductPage(pageParamsVO, platformProductVO);
    }

    @SysLog("处方流转-同步商品列表")
    @ApiOperation(value = "处方流转-同步商品列表")
    @GetMapping("synchronous/list")
    public PageVO<ThirdPartyProductVO> synchronousList(PageParamsVO pageParamsVO, ThirdPartyProductVO thirdPartyProductVO) {
        return platformService.queryPage(pageParamsVO, thirdPartyProductVO);
    }

    @SysLog("获取处方流转平台提供商品信息")
    @GetMapping("/info")
    @ApiOperation(value = "获取处方流转平台提供商品信息(添加为商品回写)")
    public PlatformSkuVO thirdPartProductInfo(Long thirdId) throws BusinessException {
        return platformService.thirdPartProductInfo(thirdId);
    }

    @SysLog("获取商品信息详情(编辑回写)")
    @GetMapping("/detail")
    @ApiOperation(value = "获取商品信息(编辑回写)")
    public PlatformSkuVO skuInfo(Long skuId) throws BusinessException {
        return platformService.skuInfo(skuId);
    }

    @SysLog("修改保存商品信息")
    @PostMapping("/update")
    @ApiOperation(value = "修改保存商品信息")
    public void saveOrUpdate(@Valid @RequestBody PlatformSkuVO param) throws BusinessException {
        platformService.updateProduct(param);
    }

    @SysLog("通过药品编码/修改时间范围添加处方平台药品")
    @GetMapping("/add")
    @ApiOperation(value = "通过药品编码/修改时间范围添加处方平台药品")
    @ApiImplicitParams({@ApiImplicitParam(name = "start", value = "开始时间", required = false, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "end", value = "结束时间", required = false, dataType = "string", paramType = "query"),
        @ApiImplicitParam(name = "numbers", value = "逗号间隔编码", required = false, dataType = "string", paramType = "query")})
    public ResponseVO<Void> addSku(String start, String end, String numbers) throws BusinessException {
        b2cPlatformFeignClient.addSku(start, end, numbers);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }
}